import { request, takeAccessToken } from '../utils/request';
import { ElMessage } from 'element-plus';

const defaultFailure = (message, status, url) => {
    console.warn(`请求地址: ${url}, 状态码: ${status}, 错误信息: ${message}`);
    ElMessage.warning(message);
};

export function getExampleData(success, failure = defaultFailure) {
    request.get('/example/data', { headers: { 'Authorization': `Bearer ${takeAccessToken()}` } })
        .then(data => {
            if (data.code === 200) {
                success(data.data);
            } else {
                failure(data.message, data.code, '/example/data');
            }
        }).catch(error => {
        console.error(error);
        ElMessage.error('发生了一些错误，请联系管理员');
    });
}

export function postExampleData(data, success, failure = defaultFailure) {
    request.post('/example/data', data, { headers: { 'Authorization': `Bearer ${takeAccessToken()}` } })
        .then(data => {
            if (data.code === 200) {
                success(data.data);
            } else {
                failure(data.message, data.code, '/example/data');
            }
        }).catch(error => {
        console.error(error);
        ElMessage.error('发生了一些错误，请联系管理员');
    });
}
